package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dao.DishMapper;
import com.itheima.reggie.dao.SetMealMapper;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetMealService;
import com.itheima.reggie.service.SetmealDishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, Setmeal> implements SetMealService {
    @Autowired
    DishService dishService;
    @Autowired
    SetmealDishService setmealDishService;

    @Autowired
    CategoryService categoryService;

    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //先保存基本数据,必须先操作setmeal表,否则无法获得套餐id,
        //无法让setmeal_dish中的setmealId和setmeal相关联
        this.save(setmealDto);
        //保存菜品和套餐关系表数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //setmeal和setmeal_dish表之间只差将菜品的setmealId和setmeal套餐表中的id关联起来
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    @Transactional
    public void deleteWithDish(Long[] ids) {
        //判断套餐是否处于售卖状态
        LambdaQueryWrapper<Setmeal> queryWrapper = Wrappers.lambdaQuery(Setmeal.class)
                .in(Setmeal::getId, Arrays.asList(ids))
                .eq(Setmeal ::getStatus,1);
        int count = this.count(queryWrapper);
        if(count > 0){
            throw new CustomerException("该套餐在售出,不可删除!");
        }
        //删除套餐
        this.removeByIds(Arrays.asList(ids));
        //删除套餐相关联的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper1 = Wrappers.lambdaQuery(SetmealDish.class)
                .in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(queryWrapper1);
    }

    @Override
    @Transactional
    public void updateStatus(Integer status, Long[] ids) {
        LambdaUpdateWrapper<Setmeal> updateWrapper = Wrappers.lambdaUpdate(Setmeal.class)
                .set(Setmeal::getStatus, status)
                .in(Setmeal::getId, Arrays.asList(ids));
         this.update(updateWrapper);
    }

    @Override
    public Page<SetmealDto> pageWithName(Integer page, Integer pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper = Wrappers.lambdaQuery(Setmeal.class)
                //模糊查询,StringUtils.isNotBlank(name),当name为空时,不做sql拼接,否则会出现错误
                .like(StringUtils.isNotBlank(name),Setmeal::getName, name)
                .orderByDesc(Setmeal::getUpdateTime);
        this.page(pageInfo,queryWrapper);

        Page<SetmealDto> pageInfo1 = new Page<>(page,pageSize);
        BeanUtils.copyProperties(pageInfo,pageInfo1,"records");
        List<SetmealDto> list = pageInfo.getRecords().stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());

        pageInfo1.setRecords(list);
        return pageInfo1;
    }
/*
*  根据套餐id查询套餐
* */
    @Override
    public SetmealDto getByIdWithDish(Long id) { // 传入套餐id
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        //select * from setmeal_dish where setmeal_id = #{id}
        LambdaQueryWrapper<SetmealDish> queryWrapper = Wrappers.lambdaQuery(SetmealDish.class)
                .eq(SetmealDish::getSetmealId, id);

        List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper);
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(setmealDishList);

        return setmealDto;
    }

    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        //先删除SetmealDish中的原有套餐菜品数据
        LambdaQueryWrapper<SetmealDish> queryWrapper = Wrappers.lambdaQuery(SetmealDish.class)
                .eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //保存菜品数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));

       setmealDishService.saveBatch(setmealDishes);

    }
}
